package com.hyz.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyz.model.asset.Asset;
import com.hyz.model.asset.AssetStatus;
import com.hyz.model.assetAllocation.AssetAllocation;
import com.hyz.model.repositoryAsset.RepositoryAsset;
import com.hyz.model.repositoryCharge.RepositoryCharge;
import com.hyz.service.AssetAllocationService;
import com.hyz.service.AssetService;
import com.hyz.service.RepositoryAssetService;
import com.hyz.service.RepositoryChargeService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/20 0:48
 */
@Component
public class JudgeUtils {
    @Autowired
    private AssetService assetService;
    @Autowired
    private RepositoryAssetService raService;
    @Autowired
    private RepositoryChargeService rcService;
    @Autowired
    private AssetAllocationService aaService;

    /**
     * 判断申请者是否有资格申请
     *
     * @param assetId 资产id
     * @param userId  申请者id
     * @return 布尔值
     */
    public Boolean checkApplicant(Long assetId, Long userId) {
        // 获取资产
        Asset asset = assetService.getById(assetId);
        if (Objects.isNull(asset)) {
            return false;
        }
        if (Objects.equals(asset.getStatus(), AssetStatus.IN_REPOSITORY)) {
            // 如果资产在库中,找到资产所在的仓库
            QueryWrapper<RepositoryAsset> raWrapper = new QueryWrapper<>();
            raWrapper.lambda().eq(RepositoryAsset::getAssetId, asset.getId());
            RepositoryAsset ra = raService.getOne(raWrapper);
            if (Objects.isNull(ra)) {
                return false;
            }
            Long repositoryId = ra.getRepositoryId();
            // 判断用户是否是对应仓库的仓库管理员
            QueryWrapper<RepositoryCharge> rcWrapper = new QueryWrapper<>();
            rcWrapper.lambda().eq(RepositoryCharge::getRepositoryId, repositoryId)
                    .eq(RepositoryCharge::getRepoManagerId, userId);
            return Objects.nonNull(rcService.getOne(rcWrapper));
        } else if (Objects.equals(asset.getStatus(), AssetStatus.SECONDMENT)) {
            // 如果资产是借调,找到借调申请
            QueryWrapper<AssetAllocation> aaWrapper = new QueryWrapper<>();
            aaWrapper.lambda().eq(AssetAllocation::getAssetId, asset.getId())
                    .eq(AssetAllocation::getApplicantId, userId);
            return Objects.nonNull(aaService.getOne(aaWrapper));
        }
        return false;
    }

    /**
     * 判断审批人员是否有资格审批
     *
     * @param assetId 资产id
     * @param userId  用户id
     * @return 是否有资格
     */

    public Boolean checkApprove(Long assetId, @NotNull Long userId) {
        // 获取资产
        Asset asset = assetService.getById(assetId);
        if (Objects.isNull(asset)) {
            return false;
        }
        if (Objects.equals(asset.getStatus(), AssetStatus.SECONDMENT)) {
            // 如果资产是借调,找到借调申请
            QueryWrapper<AssetAllocation> aaWrapper = new QueryWrapper<>();
            aaWrapper.lambda().eq(AssetAllocation::getAssetId, asset.getId())
                    .eq(AssetAllocation::getAssetManagerId, userId);
            return Objects.nonNull(aaService.getOne(aaWrapper));
        }
        return !Objects.equals(asset.getStatus(), AssetStatus.IN_REPOSITORY);
    }
}
